package com.example.projectcode.controller.Student;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.projectcode.model.*;
import com.example.projectcode.result.Result;
import com.example.projectcode.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "答题记录管理接口")
@RestController
@RequestMapping("/user/answerRecordManager")
public class AnswerRecordManager {

    @Autowired
    private CrecordService crecordService;

    @Autowired
    private ChoiceService choiceService;

    @Autowired
    private CstuanService cstuanService;

    @Autowired
    private CpstuanService cpstuanService;

    @Autowired
    private CprecordService cprecordService;

    @Autowired
    private ComprehensiveService comprehensiveService;


    @Autowired
    private TestRecordService testRecordService;

    @ApiOperation("选择题记录管理")
    @GetMapping("/choiceRecord/{StuId}")
    public Result  choiceRecord(@PathVariable Integer StuId){
        List<Crecord> crecordList = new ArrayList<>();
        crecordList = crecordService.selectCrecordByStuId(StuId);
        return Result.ok(crecordList);
    }

//    @ApiOperation()



    @ApiOperation("选择题单条记录详情")
    @GetMapping("/singleChoiceRecordDetail/{CId}/{CStuanid}")
    public Result singleChoiceRecordDetail(@PathVariable Integer CId,@PathVariable Integer CStuanid){
        Choice choice = new Choice();
        choice = choiceService.selectChoiceByCId(CId);

        Cstuan cstuan = new Cstuan();
        cstuan = cstuanService.selectOneByCStuanid(CStuanid);

       List<Cstuan> cstuanList = cstuanService.selectAllByCId(cstuan.getCId()); //所有学生改题目的答题记录
        long cstuanAll = cstuanList.stream().count();
        List<Cstuan> trueChoicecountlist = cstuanService.selectAllTrueChoiceBy(cstuan.getCId());
        long trueChoicecount = trueChoicecountlist.stream().count();
        double accuracy = (trueChoicecount * 1.0) / cstuanAll * 100;
        Map<String,Object> map = new HashMap<>();
        map.put("题目",choice.getCStem());
        map.put("学生作答情况",cstuan.getCStuanc());
        map.put("答案",choice.getCAnswer());
        map.put("全体学生用户该题正确率",accuracy);

        return Result.ok(map);



    }

    @ApiOperation("选择题记录删除")
    @GetMapping("deleteChoiceRecord/{CRecordid}")
    public Result deleteChoiceRecord(@PathVariable Integer CRecordid){
        int result = crecordService.deleteChoiceRecordByCRecordid(CRecordid);
        if (result>0){
            return Result.ok().message("删除成功");
        }else {
            return Result.fail().message("删除失败");
        }
    }


    @ApiOperation("综合题记录管理")
    @GetMapping("/comprehensiveRecord/{StuId}")
    public Result  comprehensiveRecord(@PathVariable Integer StuId){
        List<Crecord> crecordList = new ArrayList<>();
        crecordList = crecordService.selectCrecordByStuId(StuId);
        return Result.ok(crecordList);
    }

    @ApiOperation("综合题单条记录详情")
    @GetMapping("/singleComprehensiveDetail/{CpId}/{CpStuanid}")
    public Result singleComprehensiveDetail(@PathVariable Integer CpId,@PathVariable Integer CpStuanid){
        Comprehensive comprehensive = new Comprehensive();
        comprehensive = comprehensiveService.selectComprehensiveByCpId(CpId);

        Cpstuan cpstuan = new Cpstuan();
        cpstuan = cpstuanService.selectCpstuanByCpstuanid(CpStuanid);

        Map<String,Object> map = new HashMap<>();
        map.put("题目",comprehensive.getCpStem());
        map.put("学生作答情况",cpstuan.getCpStuanc());
        map.put("答案",comprehensive.getCpAnswer());
        return Result.ok(map);
    }


    @ApiOperation("选择题错题记录管理")
    @GetMapping("/wrongchoiceRecord/{StuId}")
    public Result  WrongchoiceRecord(@PathVariable Integer StuId){
        List<Cstuan> cstuanList = new ArrayList<>();
        cstuanList = cstuanService.selectAllByStuId(StuId);

        return Result.ok(cstuanList);

    }

    @ApiOperation("选择题单条错题记录详情")
    @GetMapping("/wrongsingleChoiceRecordDetail/{StuId}")
    public Result WrongsingleChoiceRecordDetail(@PathVariable Integer StuId){
        List<Cstuan> cstuanList = new ArrayList<>();
        cstuanList = cstuanService.selectAllByStuId(StuId);
        Map<String,Object> map = new HashMap<>();
        for (Cstuan cstuan: cstuanList){
            Choice choice = new Choice();
            choice = choiceService.selectChoiceByCId(cstuan.getCId());

            List<Cstuan> cstuans = cstuanService.selectAllByCId(cstuan.getCId()); //所有学生改题目的答题记录
            long cstuanAll = cstuans.stream().count();
            List<Cstuan> trueChoicecountlist = cstuanService.selectAllTrueChoiceBy(cstuan.getCId());
            long trueChoicecount = trueChoicecountlist.stream().count();
            double accuracy = (trueChoicecount * 1.0) / cstuanAll * 100;
            map.put("选择题题目",choice.getCStem());
            map.put("学生作答答案",cstuan.getCStuanc());
            map.put("选择题答案",choice.getCAnswer());
            map.put("全体学生用户该题正确率",accuracy);
        }
        return Result.ok(map);

    }


    @ApiOperation("综合题错题记录管理")
    @GetMapping("/wrongcomprehensiveRecord/{StuId}")
    public Result  WrongComprehensiveRecord(@PathVariable Integer StuId){
        List<Cpstuan> cprecords = new ArrayList<>();
        cprecords = cpstuanService.selectAllfalsecomprehensiveByStuId(StuId);

        return Result.ok(cprecords);
    }


 /*-------------------------------------------------新加-----------------------------------------*/


    @ApiOperation("根据学生id分页查询所有选择题记录")
    @GetMapping("queryAllCRecord/{stuId}/{page}/{size}")
    public Result queryAllCRecord(@PathVariable Integer stuId,
                                  @PathVariable long page,
                                  @PathVariable long size){
        Page<Crecord> crecordPage = new Page<>(page,size);
        IPage<Crecord> crecordIPage = crecordService.page(crecordPage,
                new QueryWrapper<Crecord>().eq("Stu_id",stuId));
        return Result.ok(crecordIPage);

    }

    @ApiOperation("根据学生id查询所有综合题记录")
    @GetMapping("/queryAllCpRecord/{stuId}/{page}/{size}")
    public Result queryAllCpRecord(@PathVariable Integer stuId,
                                   @PathVariable long page,
                                   @PathVariable long size){
        Page<Cprecord> cprecordPage = new Page<>(page,size);
        IPage<Cprecord> cprecordIPage = cprecordService.page(cprecordPage,new QueryWrapper<Cprecord>().eq("Stu_id",stuId));
        return Result.ok(cprecordIPage);
    }



    @ApiOperation("根据学生id分页查询学生考试记录")
    @GetMapping("/queryAllTestRecord/{stuId}/{page}/{size}")
    public Result queryAllTestRecord(@PathVariable Integer stuId,@PathVariable long page,@PathVariable long size){
        Page<TestRecord> testRecordPage = new Page<>(page,size);
        IPage<TestRecord> testRecordIPage = testRecordService.page(testRecordPage,
                new QueryWrapper<TestRecord>().eq("Stu_id",stuId));

        return Result.ok(testRecordIPage);
    }









}
